package com.bruce.exec;

import cn.hutool.core.lang.Console;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.system.SystemUtil;
import com.bruce.exec.utils.ProcessUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * @author xin.ding
 * @date 2021/7/6 4:52 下午
 */
public class JmxJar {

    public static void main(String[] args) {
        String ipAddress = NetUtil.getLocalhostStr();
        String port = "9999";
        System.setProperty("com.sun.management.jmxremote", "true");
        System.setProperty("com.sun.management.jmxremote.authenticate", "false");
        System.setProperty("com.sun.management.jmxremote.ssl", "false");
        System.setProperty("com.sun.management.jmxremote.port", port);
        Console.log("启动JMX监控: host={} port={} pid={}", ipAddress, port, SystemUtil.getCurrentPID());

        String systemProperty1 = SystemUtil.get("com.sun.management.jmxremote");
        String systemProperty2 = SystemUtil.get("com.sun.management.jmxremote.authenticate");
        String systemProperty3 = SystemUtil.get("com.sun.management.jmxremote.ssl");
        String systemProperty4 = SystemUtil.get("com.sun.management.jmxremote.port");
        Console.log("{} {} {} {}", systemProperty1, systemProperty2, systemProperty3, systemProperty4);

        String cmd = "java -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=8888 -jar exec.jar";
        Process exec = RuntimeUtil.exec(cmd);
        Console.log(cmd);
        ProcessUtils.getProcessOutput(exec);
        String pid = ProcessUtils.getPid(exec);
        Console.log("启动任务Pid:{}", pid);
        try {
            int i = exec.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Console.log("程序结束");

    }
}

